<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button-group>
        <el-button :type="filterForm.remove == 1?'success':'default'" @click="filterForm.remove=1;">全部</el-button>
        <el-button :type="filterForm.remove == 3?'success':'default'" @click="filterForm.remove=3">我的运单</el-button>
        <el-button :type="filterForm.remove == 2?'success':'default'" @click="filterForm.remove=2;">回收站</el-button>
      </el-button-group>
    </div>
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable="true" placeholder="运单ID/运单标题/内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="发货时间" prop="waybillTime">
          <el-date-picker 
          clearable="true"
          value-format="yyyy-MM-dd"
            v-model="filterForm.publishTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="寄件人" prop="senderID">
            <el-select v-model="filterForm.senderID" clearable="true" placeholder="请选择">
              <el-option v-for="item in catalogueOptions" :key="item.ID" :label="item.title" :value="item.ID" />
            </el-select>
        </el-form-item>
        <el-form-item label="快递公司" prop="companyID">
            <el-select v-model="filterForm.companyID" clearable="true" placeholder="请选择">
              <el-option v-for="item in originateOptions" :key="item.ID" :label="item.title" :value="item.ID" />
            </el-select>
        </el-form-item>
        <el-form-item label="发货人" prop="employee">
          <el-input v-model="filterForm.employee" clearable="true" placeholder="发货人ID/发货人电话/发货人别名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="filterForm.status">
            <el-radio-button border label="0">全部</el-radio-button>
            <el-radio-button border label="3">发放中</el-radio-button>
            <el-radio-button border label="4">已结束</el-radio-button>
            <el-radio-button border label="2">未开始</el-radio-button>
            <el-radio-button border label="1">编辑中</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
              新建
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleRemove">
              删除
            </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="运单号" width="200">
        <template slot-scope="{row}">
          <span>{{ row.waybillID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品" min-width="150px">
        <template slot-scope="{row}">
        </template>
      </el-table-column>
      <el-table-column label="收件人" max-width="150px">
        <template slot-scope="{row}">
          <div>联系人：{{ row.fullname }}</div>
          <div>电话号码：{{ row.telephone }}</div>
          <div>详细地址：{{ row.address }}</div>
        </template>
      </el-table-column>
      <el-table-column label="快递单号" max-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.shipperCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="320" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="success" @click="handeUpdate(row)">
            修改
          </el-button>
          <el-button  @click="handleWaybillCopy(row)">
            复制
          </el-button>
          <el-button  @click="handleModifyStatus(row,5)">
            打印
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value === 4 || row.status.value == 2 || row.status.value == 5" size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />
  </div>
</template>

<script>
import { getWaybillList, changeStatusByWaybillId, removeWaybillById,copyByWaybillID } from '@/api/logistics/waybill'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      filterForm: {
        start: 1,
        perpage: 10,
        waybill: undefined,
        sort: '+id'
      },
      list: null,
      total: 0,
      waybillForm: {

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCreate() {
      this.$router.push({ path: 'waybill-add' })
    },
    handeUpdate(item) {
      this.$router.push({ path: 'waybill-add', query: { 'waybillID': item.ID }})
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getWaybillList(this.filterForm).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.filterForm.start = 1
      this.getList()
    },
    handleWaybillCopy:function(row){
      const _this = this
      copyByWaybillID(row.ID).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleModifyStatus(row, status) {
      const _this = this
      const formData = {
        'waybillID': row.ID,
        'status': status
      }
      changeStatusByWaybillId(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.filterForm.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const _this = this
      const formData = {
        'waybillID': row.ID,
      }
      removeWaybillById(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.filterForm.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
